
package com.putao.album.main;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.putao.album.R;
import com.putao.album.base.BaseActivity;
import com.putao.album.popupwindow.PtInvitePopupWindow;
import com.putao.album.util.UmengTouchEvent;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by yanglun on 15-4-27.
 */
public class ActivityInviteChildRelationSelect extends BaseActivity implements PtInvitePopupWindow.PopupClickListener {
    private TextView title_tv;
    private LinearLayout ll_relation_list;
    private PopupWindow popupwindow;
    String mInviteRelationShip = "爸爸";
    private String[] relationShips = new String[]{"妈妈", "爸爸", "外婆", "外公", "爷爷", "奶奶", "阿姨", "叔叔", "姑姑", "舅舅", "PUTAO_RELATION"};


    private int[] relationShipTexts = new int[]{
            R.string.child_reation_mun,
            R.string.child_reation_dad,
            R.string.child_reation_grandma,
            R.string.child_reation_grandpa,
            R.string.child_reation_grandfather,
            R.string.child_reation_grandmother,
            R.string.child_reation_aunt,
            R.string.child_reation_uncle,
            R.string.child_reation_aunt_2,
            R.string.child_reation_uncle_2,
            R.string.child_reation};
    private int[] relationShipIcons = new int[]{
            R.drawable.baby_icon_mom,
            R.drawable.baby_icon_dad,
            R.drawable.baby_icon_grandma,
            R.drawable.baby_icon_grandpa,
            R.drawable.baby_icon_grandfather,
            R.drawable.baby_icon_grandmother,
            R.drawable.baby_icon_aunt01,
            R.drawable.baby_icon_uncle01,
            R.drawable.baby_icon_aunt02,
            R.drawable.baby_icon_uncle02,
            R.drawable.baby_icon_other};

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_btn:
                finish();
                break;
        }
    }

    public void showPopupView(View v) {
        if (popupwindow != null && popupwindow.isShowing()) {
            popupwindow.dismiss();
            popupwindow = null;
        } else {
            popupwindow = PtInvitePopupWindow.Create(mActivity, this);
            popupwindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
        }
    }

    @Override
    public int doGetContentViewId() {
        return R.layout.activity_invite_child_relation_select;
    }

    @Override
    public void doInitSubViews(View view) {
        title_tv = (TextView) findViewById(R.id.title_tv);
        title_tv.setText("邀请家人");
        queryViewById(R.id.right_btn).setVisibility(View.INVISIBLE);
        queryViewById(R.id.back_btn, true);
        ll_relation_list = queryViewById(R.id.ll_relation_list);
        addRelationUI();
    }

    /**
     * 添加关系列表
     */
    void addRelationUI() {
        for (int i = 0; i < relationShips.length; i++) {
            LinearLayout layout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.activity_invite_child_relation_select_item, null);
            queryViewById(layout, R.id.image_icon).setBackgroundResource(relationShipIcons[i]);
            ((TextView) queryViewById(layout, R.id.tv_relation)).setText(relationShipTexts[i]);
            layout.setTag(relationShips[i]);
            layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mInviteRelationShip = (String) v.getTag();
                    doUmengEvent();
                    showPopupView(v);
                }
            });
            ll_relation_list.addView(layout);
            ll_relation_list.requestLayout();
        }
    }

    void doUmengEvent() {
        String key = UmengTouchEvent.INVITE_FRIEND;
        if (mInviteRelationShip.equals("妈妈")) {
            key = UmengTouchEvent.INVITE_MOTHER;
        } else if (mInviteRelationShip.equals("爸爸")) {
            key = UmengTouchEvent.INVITE_FATHER;
        } else if (mInviteRelationShip.equals("外婆") || (mInviteRelationShip.equals("奶奶"))) {
            key = UmengTouchEvent.INVITE_GRANDMOTHER;
        } else if (mInviteRelationShip.equals("外公") || (mInviteRelationShip.equals("爷爷"))) {
            key = UmengTouchEvent.INVITE_GRANDFATHER;
        }
        MobclickAgent.onEvent(mContext, key);
    }

    @Override
    public void doInitData() {
        if (getIntent() != null) {
            Bundle receiveBundle = getIntent().getExtras();
            if (receiveBundle != null) {
                String relation = receiveBundle.getString("relation");
            }
        }
    }

    @Override
    public String getRelateionShip() {
        return mInviteRelationShip;
    }
}
